package org.bits.ui;

import java.awt.*;

import javax.swing.*;

import org.bits.data.Issue;

/*******************************************************************************
 * 
 ******************************************************************************/
public class IssueView
{
    /**  */
    private JPanel panel;
    /**  */
    private JTextField numberField;
    /**  */
    private JTextField titleField;
    /**  */
    private JEditorPane descriptionField;
    /**  */
    private boolean isNew;
    /**  */
    private Issue issue;

    /***************************************************************************
     * 
     **************************************************************************/
    public IssueView()
    {
        this( false );
    }

    /***************************************************************************
     * @param isNew
     **************************************************************************/
    public IssueView( boolean isNew )
    {
        this.isNew = isNew;

        numberField = new JTextField( 20 );
        titleField = new JTextField( 20 );
        descriptionField = new JEditorPane();

        panel = createFormPanel();
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    private JPanel createFormPanel()
    {
        JPanel panel = new JPanel( new GridBagLayout() );
        JScrollPane descriptionPane = new JScrollPane( descriptionField );
        JLabel numberLabel = new JLabel( "Number" );
        GridBagConstraints constraints;

        numberLabel.setVisible( !isNew );
        numberField.setEditable( false );
        numberField.setVisible( !isNew );

        descriptionPane.setPreferredSize( new Dimension( 300, 200 ) );
        descriptionPane.setMinimumSize( new Dimension( 300, 200 ) );

        constraints = new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 4, 4,
                2, 4 ), 0, 0 );
        panel.add( numberLabel, constraints );
        constraints = new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 2, 4,
                4, 4 ), 0, 0 );
        panel.add( numberField, constraints );

        constraints = new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 4, 4,
                2, 4 ), 0, 0 );
        panel.add( new JLabel( "Title" ), constraints );
        constraints = new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(
                2, 4, 4, 4 ), 0, 0 );
        panel.add( titleField, constraints );

        constraints = new GridBagConstraints( 0, 4, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 4, 4,
                2, 4 ), 0, 0 );
        panel.add( new JLabel( "Description" ), constraints );
        constraints = new GridBagConstraints( 0, 5, 1, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(
                2, 4, 4, 4 ), 0, 0 );
        panel.add( descriptionPane, constraints );

        constraints = new GridBagConstraints( 0, 25, 1, 1, 1.0, 1.0,
            GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 0,
                0, 0 ), 0, 0 );
        panel.add( Box.createVerticalStrut( 0 ), constraints );

        return panel;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    public JPanel getView()
    {
        return panel;
    }

    /***************************************************************************
     * @param issue
     **************************************************************************/
    public void setIssue( Issue issue )
    {
        this.issue = issue;
    }

    public void apply()
    {
        issue.title = titleField.getText();
        issue.description = descriptionField.getText();
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    public Issue getIssue()
    {
        return issue;
    }
}
